<template>
  <div class="app-container">
    <el-card shadow="never" class="table-container">
      <template #header> </template>
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        :stripe="true"
        size="large"
        max-height="600px"
      >
        <el-table-column align="center" label="序号" type="index" width="100" />

        <el-table-column align="center" label="预约用户" prop="bookUser" />
        <el-table-column align="center" label="实验室" prop="labName" />
        <el-table-column align="center" label="座位编号" prop="seatNo" />

        <el-table-column align="center" label="开始时间" prop="startTime" />
        <el-table-column align="center" label="结束时间" prop="endTime" />

        <el-table-column align="center" label="状态" width="120" prop="status">
          <template #default="scope">
            <el-tag v-if="scope.row.status === 0" type="warning">待审核</el-tag>
            <el-tag v-if="scope.row.status === 1" type="danger">驳回</el-tag>
            <el-tag v-if="scope.row.status === 2" type="success">通过</el-tag>
            <el-tag v-if="scope.row.status === 3">完成</el-tag>
            <el-tag v-if="scope.row.status === 4" type="info">取消</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-popconfirm
              v-if="scope.row.status === 0 || scope.row.status === 2"
              title="你确定需要取消预约?"
              @confirm="cancel(scope.row.id)"
            >
              <template #reference>
                <el-button type="danger" text size="small">
                  取消预约
                </el-button>
              </template>
            </el-popconfirm>
            <el-button
              type="primary"
              v-if="scope.row.status === 2"
              text
              size="small"
              @click="finish(scope.row.id)"
            >
              确定完成
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { getMyRecordAPI } from "@/api/bookRecord/index";
import { cancelAPI, finishAPI } from "@/api/book/index";

import { IPage, IBookRecordVo } from "@/api/bookRecord/types";

import { ref, reactive } from "vue";
/* import { getCountryBaseListBookRecordAPI } from "@/api/countryBase/index";
import { CountryBase } from "@/api/countryBase/types"; */

const total = ref(0);
const loading = ref(false);
const tableData = ref<IBookRecordVo[]>([]);
const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);
// 表单数据
const formData = reactive<IBookRecordVo>({});
// 表单规则
const rules = reactive({
  title: [{ required: true, message: "请输入", trigger: "blur" }],
});

const dialog = reactive<DialogOption>({
  visible: false,
});
const queryParams = reactive<IPage>({
  pageNum: 1,
  pageSize: 10,
});

onMounted(() => {
  handleQuery();
});

// 获取预约记录列表
function handleQuery() {
  loading.value = true;
  getMyRecordAPI()
    .then(({ data }) => {
      tableData.value = data.filter((item) => {
        if (item.status === 0 || item.status === 2) {
          return true;
        } else {
          return false;
        }
      });
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */

function resetQuery() {
  queryParams.pageNum = 1;
  handleQuery();
}

/** 关闭表单弹窗 */
function closeDialog() {
  dialog.visible = false;
  resetForm();
}

/** 重置表单 */
function resetForm() {
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.id = undefined;
}
/** 取消成功 */
function cancel(id: number) {
  loading.value = true;
  cancelAPI(id)
    .then(() => {
      ElMessage.success("取消成功");
      resetQuery();
    })
    .finally(() => (loading.value = false));
}

/** 完成 */
function finish(id: number) {
  loading.value = true;
  finishAPI(id)
    .then(() => {
      ElMessage.success("确认完成");
      resetQuery();
    })
    .finally(() => (loading.value = false));
}
</script>

<style lang="scss" scoped>
.title {
  width: 100%;
  text-align: center;
  font-size: 20px;
  color: #377498;
  font-weight: 700;
}
</style>
